﻿using MediatR;
using SmartProduction.Domain;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.BomCommand;

namespace SmartProduction.Read.API.Application.CommandHandlers.BomCommandHandler
{
    /// <summary>
    /// 处理操作记录列表命令
    /// </summary>
    public class OperationRecordQueryCommandHandler : IRequestHandler<OperationRecordQueryCommand, APIResult<List<OperationRecord>>>
    {
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IBaseRepository<OperationRecord> operaRepository;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="operaRepository">仓储</param>
        public OperationRecordQueryCommandHandler(IBaseRepository<OperationRecord> operaRepository)
        {
            this.operaRepository = operaRepository;
        }
       /// <summary>
       /// /处理
       /// </summary>
       /// <param name="request"></param>
       /// <param name="cancellationToken"></param>
       /// <returns></returns>
       /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<List<OperationRecord>>> Handle(OperationRecordQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<OperationRecord>> result = new APIResult<List<OperationRecord>>();
            var entity = operaRepository.GetAll().Result;
            if (request.BomId != 0)
            {
                entity = entity.Where(x => x.BomId == request.BomId);
            }
            result.Code = APIEnums.查询成功;
            result.Msg = APIEnums.查询成功.ToString();
           result.data=entity.ToList();
            return  Task.FromResult(result);
        }
    }
}
